<?php
namespace Admin\Controller;
use Think\Controller;

class AdmingroupController extends Controller {

	function __construct() {
		//继承父类
		parent::__construct();

		//判断登录状态
		if(!D('Admin')->islogin()){//未登录
			$this->error('您尚未登录，请先登录！',U('login/login'),3);
		}
		//判断操作是否授权
		if(!D('Admin')->isoperate(CONTROLLER_NAME,ACTION_NAME)){//未授权
			$this->error('您尚无此操作权限！',U('index/welcome'),3);
		}
    }

	//空方法，防止报错
	public function _empty(){
        $this->index();
    }
	
	//管理员组列表
    public function index(){
		$where="1 ";
		$info=I("info");
		if ($info){
			@extract($info);
			if($keyword){$where.="and group_name like '%$keyword%' ";$this->assign('keyword',$keyword);}
		}

		$admingroup=M('Admingroup');
		// 查询满足要求的总记录数
		$count      = $admingroup->where($where)->count();
		$Page       = new \Think\Page($count,15);
		//分页跳转的时候保证查询条件
		if($info){
			foreach($info as $key=>$val) {
				$Page->parameter['info['.$key.']']   =   urlencode($val);
			}
		}
		$pageshow   = $Page->adminshow();
		// 进行分页数据查询
		$listinfo = $admingroup->where($where)->order('group_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('listinfo',$listinfo);
		$this->assign('pageshow',$pageshow);

		$this->display();
    }

	//添加管理员组
    public function addadmingroup(){
		if (IS_POST){
			$info=I("info");
			$info['group_name']=trim($info['group_name']);
			$menu_id=$info['menu_id'];
			$info['group_menu']=join(",",$menu_id);
			unset($info['menu_id']);
			$flag=M("Admingroup")->add($info);
			if($flag){
				$this->success('添加管理员组成功！',U('admingroup/index'),3);
			}else{
				$this->error('添加管理员组失败！',U('admingroup/index'),3);
			}
		}else{
			$menuinfo=D("Menuinfo")->getmenuninfo(0);
			$this->assign('menuinfo',$menuinfo);
			$this->display();
		}
    }

	//编辑管理员组
    public function editadmingroup(){
		$group_id=I("get.group_id");
		if (IS_POST){
			$info=I("info");
			$info['group_name']=trim($info['group_name']);
			$menu_id=$info['menu_id'];
			$info['group_menu']=join(",",$menu_id);
			unset($info['menu_id']);
			$flag=M("Admingroup")->where("group_id=".$group_id)->save($info);
			if($flag){
				$this->success('编辑管理员组成功！',U('admingroup/index'),3);
			}else{
				$this->error('编辑管理员组失败！',U('admingroup/index'),3);
			}
		}else{
			$groupinfo=M("Admingroup")->where("group_id=".$group_id)->find();
			$this->assign('groupinfo',$groupinfo);

			$menuinfo=D("Menuinfo")->getmenuninfo(0);
			$this->assign('menuinfo',$menuinfo);

			//$menu_id=explode(",",$groupinfo['group_menu']);
			//$this->assign('menu_id',$menu_id);
			$this->display();
		}
    }

	//删除管理员
	public function deladmingroup(){
		$group_id=I("get.group_id");
		M('Admingroup')->where('group_id='.$group_id)->delete();
		$this->success('管理员组删除成功！',U('admingroup/index'),3);
	}

}
